package com.mceil.mylibrary.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.mceil.mylibrary.annotation.ContentView;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment {
    private View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(getLayoutRes(), container, false);
            ButterKnife.bind(this, view);
            initData(savedInstanceState);
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    //加载缓存信息
    protected abstract void initPreferences();

    protected abstract void successViewCompleted(View successView);

    /**
     * 初始化数据
     *
     * @param savedInstanceState 保存状态数据使用
     */
    public abstract void initData(Bundle savedInstanceState);


    protected int getLayoutRes() {
        Class<?> handlerType = this.getClass();
        ContentView contentView = handlerType.getAnnotation(ContentView.class);
        if (contentView == null) {
            return 10;
        }
        return contentView.value();
    }
}
